(function() {
  var express, iced, pgsql, router, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  router = express.Router();

  module.exports = router;

  router.all('/', function(req, res, next) {
    var client, done, err, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 11
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        where_clause = "( f.name like $1 or f.notes like $1 )";
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
          });
          client.query("select count(f.*) as count from pay_fee as f where " + where_clause, ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 22
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.total = result.rows[0].count;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
            });
            client.query("select f.*, count(distinct m.uuid) + count(distinct p2.uuid) as n_refs from pay_fee as f left join pay_merch as m on m.t20_fee = f.uuid left join pay_p2 as p2 on p2.t60_fee = f.uuid where " + where_clause + " group by f.serial, f.uuid order by f.serial offset $2::int limit $3::int", ['%' + res.locals.keyword + '%', res.locals.offset, res.locals.page_rows], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 37
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.render('fee/index', {
              records: result.rows
            });
          });
        });
      };
    })(this));
  });

  router.get('/new', function(req, res, next) {
    res.locals.breadcrumbs.push({
      'title': '新增手续费规则'
    });
    return res.render('fee/new', {
      record: {}
    });
  });

  router.post('/new', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['name', 'debit_rules', 'cedit_rules'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 52
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
          });
          client.query("insert into pay_fee ( uuid, name, debit_rules, cedit_rules, notes ) values ( $1, $2, $3, $4, $5 )", [uuid.v4(), req.body.name, req.body.debit_rules, req.body.cedit_rules, req.body.notes], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 66
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.redirect('/fee');
        });
      };
    })(this));
  });

  router.get('/edit/:id', function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '修改手续费规则'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 75
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
          });
          client.query("select * from pay_fee where serial = $1 or uuid = $2", [parseInt(req.params.id) || 0, req.params.id], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 81
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length !== 1) {
            return next(new Error("查无此手续费定义[" + req.params.id + "]"));
          }
          return res.render('fee/edit', {
            record: result.rows[0]
          });
        });
      };
    })(this));
  });

  router.post('/edit', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['serial', 'uuid', 'debit_rules', 'cedit_rules'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 94
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
          });
          client.query("update pay_fee set name = $1, debit_rules = $2, cedit_rules = $3, notes = $4 where serial = $5 and uuid = $6", [req.body.name, req.body.debit_rules, req.body.cedit_rules, req.body.notes, req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 107
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.redirect('/fee');
        });
      };
    })(this));
  });

  router.post('/delete', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial', 'uuid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 118
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/fee.coffee"
          });
          client.query("delete from pay_fee where serial = $1 and uuid = $2", [req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 124
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.json({
            succ: true,
            redirect: '/fee'
          });
        });
      };
    })(this));
  });

}).call(this);
